__asm__(".code16gcc");

/* output byte */
void outb(unsigned int ad,unsigned char v){
	__asm__("outb %%al, %%dx" :: "d" (ad), "a" (v));
}
/* input byte */
unsigned char inb(unsigned int ad){
	unsigned char _v;
	__asm__("inb %%dx, %%al" : "=a" (_v) : "d" (ad));
	return _v;
}

unsigned short inw(unsigned int ad){
	unsigned short _v;			\
	__asm__("inw %%dx, %%ax" : "=a" (_v) : "d" (ad));	\
	return _v;
}

#define LBASECTOR 1
#define LBASIZE 20
#define LoadAddr 0x10000

//复制从扇区LBASECTOR开始的LBASIZE个扇区到LoadAddr开始地址中

void read_harddisk()
{
	unsigned char LBASECTOR0 = (unsigned char)(LBASECTOR&0x000000ff);
	unsigned char LBASECTOR1 = (unsigned char)((LBASECTOR&0x0000ff00)>>8);
	unsigned char LBASECTOR2 = (unsigned char)((LBASECTOR&0x00ff0000)>>16);
	unsigned char LBASECTOR3 = (unsigned char)((LBASECTOR&0xff000000)>>24);
	outb(0x1f2,LBASIZE); //1sector
	outb(0x1f3,LBASECTOR0);
	outb(0x1f4,LBASECTOR1);
	outb(0x1f5,LBASECTOR2);
	LBASECTOR3 = (LBASECTOR3&0x0f) | 0xe0; //master disk LBA
	outb(0x1f6,LBASECTOR3);
	outb(0x1f7,0x20);
	while(1){
		unsigned char data = inb(0x1f7);
		if(0x08 == (data&0x88)){
			break;
		}
	}
	/*数据段指向0x1000*/
	__asm__ ("movw $0x1000,%ax\n\t"
			 "movw %ax,%ds\n"
			);
	unsigned short *pDst = (unsigned short *)(0);
	int index = 0;
	/*复制*/
	for(; index < 256*LBASIZE; index++){
		unsigned short data = inw(0x1f0);
		*pDst++ = data;
	}
}